/*
 * Copyright (c) 2017,安徽皖通科技股份有限公司 All rights reserved.
 */

package com.wttech.dzzf.process.service.upload;

import cn.com.taiji.sdk.comm.ETCCommHelper;
import cn.com.taiji.sdk.model.comm.protocol.transaction.RechargeUploadRequest;
import cn.com.taiji.sdk.model.comm.protocol.transaction.ReversalUploadRequest;
import cn.com.taiji.sdk.model.comm.protocol.transaction.ReversalUploadResponse;
import com.wttech.dzzf.process.constant.UserConstants;
import com.wttech.dzzf.process.dao.ReverSalInfoDao;
import com.wttech.dzzf.process.service.user.UserDetailInfosCreatorService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;

/**
 * 冲销信息上传服务实现
 *
 * @author MaShibo
 * @date 2017/11/13.
 */
@Service
public class ReversalInfosUploadService {

    private static final Logger LOGGER = LoggerFactory.getLogger(ReversalInfosUploadService.class);

    @Resource
    private ReverSalInfoDao reverSalInfoDao;

    @Resource
    private ThreadPoolTaskExecutor executor;

    @Resource
    private UserDetailInfosCreatorService userDetailInfosCreatorService;

    @Resource
    private RechargeInfosUploadService rechargeInfosUploadService;


    private void process() {
        List<ReversalUploadRequest> reversals = reverSalInfoDao.queryReversalUploadInfos();
        LOGGER.info("共{}条冲正数据待处理", reversals.size());
        List<Future<Void>> futures = new ArrayList<>(reversals.size());
        reversals.forEach(reversal -> futures.add(executor.submit(new SubProcessThread(reversal))));
        userDetailInfosCreatorService.getFutureResult(futures);
        LOGGER.info("执行完毕");
        executor.shutdown();
    }


    private class SubProcessThread implements Callable<Void> {

        private ReversalUploadRequest reversal;

        public SubProcessThread(ReversalUploadRequest reversal) {
            this.reversal = reversal;
        }

        @Override
        public Void call() throws Exception {
            processSingleReversal(reversal);
            return null;
        }

        private void processSingleReversal(ReversalUploadRequest reversal) {
            try {
                String reversalId = reversal.getId();
                LOGGER.info("正在处理{}", reversalId);
                String rechargeId = reversalId.substring(0, reversalId.length() - 1);
                RechargeUploadRequest recharge = reverSalInfoDao.queryRechargeUploadInfo(rechargeId);
                if (recharge != null) {
                    rechargeInfosUploadService.processSingleRecharge(recharge);

                    ReversalUploadResponse reversalRsp = ETCCommHelper.upload(reversal.getFileName(UserConstants.DEFAULT_PROVINCE_CODE), reversal, ReversalUploadResponse.class);
                    LOGGER.info("冲正响应消息{}", reversalRsp.getInfo());
                }
            } catch (Exception e) {
                LOGGER.error(e.getMessage());
            }
        }
    }

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        ReversalInfosUploadService service = context.getBean(ReversalInfosUploadService.class);
        service.process();
    }
}
